//
//  ConversationViewCell.m
//  EaseMob
//
//  Created by 王方伟 on 16/7/27.
//  Copyright © 2016年 wfw. All rights reserved.
//

#import "ConversationViewCell.h"
#import "EMVoiceMessageBody.h"
#import "NSDate+Extend.h"
#import "BadgeButton.h"

@interface ConversationViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UILabel *titleL;
@property (weak, nonatomic) IBOutlet UILabel *subtitleL;
@property (weak, nonatomic) IBOutlet UILabel *timeL;
@property (weak, nonatomic) IBOutlet BadgeButton *bageBtn;

@end

@implementation ConversationViewCell

- (EMConversation *)getNewConversation{
     EMConversation *conversaion = _model.conversation;
    if (![conversaion.ext objectForKey:@"subject"]){
        NSArray *groupArray = [[EMClient sharedClient].groupManager getAllGroups];
        for (EMGroup *group in groupArray) {
            if ([group.groupId isEqualToString:conversaion.conversationId]) {
                NSMutableDictionary *ext = [NSMutableDictionary dictionaryWithDictionary:conversaion.ext];
                [ext setObject:group.subject forKey:@"subject"];
                [ext setObject:[NSNumber numberWithBool:group.isPublic] forKey:@"isPublic"];
                conversaion.ext = ext;
                break;
            }
        }
    }
    return conversaion;
}

- (void)setModel:(MeMConversation *)model{
    _model = model;
    EMConversation *conversaion = _model.conversation;
    
    if (conversaion.type == EMConversationTypeGroupChat) {
        NSDictionary *ext = [self getNewConversation].ext;
        _headImage.image = [UIImage imageNamed:@"other"];
        _titleL.text = [ext objectForKey:@"subject"];
        model.title = [ext objectForKey:@"subject"];
    }else{
        _titleL.text = conversaion.conversationId;
        model.title = conversaion.conversationId;
        _headImage.image = [UIImage imageNamed:@"me"];
    }
    
    EMMessage *message = conversaion.latestMessage;
    EMMessageBody *body = message.body;
    _timeL.text = [[NSDate dateWithTimeIntervalInMilliSecondSince1970:message.timestamp] formattedTime];
    [_bageBtn badgeValue:[NSString stringWithFormat:@"%d", conversaion.unreadMessagesCount] finishBlock:^{
        MyLog(@"badgeValue:%d", conversaion.unreadMessagesCount);
    }];
    
    switch (body.type) {
        case EMMessageBodyTypeText://文本类型
        {
            EMTextMessageBody *textBody = (EMTextMessageBody *)body;
            _subtitleL.text = textBody.text;
        }
            break;
        case EMMessageBodyTypeImage://图片类型
        {
            EMImageMessageBody *imageBody = (EMImageMessageBody *)body;
            _subtitleL.text = imageBody.displayName;
        }
            break;
            
        case EMMessageBodyTypeLocation://位置类型
            
            break;
            
        case EMMessageBodyTypeVoice://语音类型
        {
            EMVoiceMessageBody *voiceBody = (EMVoiceMessageBody *)body;
            _subtitleL.text = voiceBody.displayName;
        }
            break;
            
        case EMMessageBodyTypeVideo://位置类型
            
            break;
            
        case EMMessageBodyTypeFile://文件类型
            
            break;
            
        case EMMessageBodyTypeCmd://命令类型
            break;
            
    }
}

- (void)setConversaion:(MeMConversation *)conversaion{
  
    
}

@end
